import * as constants from './constants';
import { fromJS } from 'immutable';
import { message } from 'antd';
import { saveNewCateRegFriPage, saveNewCateRegSecPage,
  getResourceInfo, getResourceTableInfo,
   getAllMetaDataTypes, getNewResBaseInfo,
  deleteResourceById,
  postResource} from '../../../../api/resourceCategory/resourceCategory'

export const setResTableRow = (value, key, inputType) => ({
  type: constants.SETRESTABLEROWINFO,
  value, key, inputType
})
export const addRestable = (count) => ({
  type: constants.SETRESTABLECOUNT,
  count
})
export const delResTableRow = (key) => ({
  type: constants.DELRESTABLEROW,
  key
})

//设置表单初始值 ，改变reducer里的initialData
export const setFormInitialData  = (data) => ({
  type: constants.SETFORMINITIALDATA,
  data: fromJS(data)
})

export const saveCateRegFri = (data) => {
  return dispatch => {
    saveNewCateRegFriPage(data).then(res => {
      if(res.data.metaData.code === 'success') {
        message.success('添加成功')
      }
      else {
        message.error(res.data.metaData.message)
      }
    })
  }
}

export const saveCateRegSec = (data) => {
  return dispatch => {
    saveNewCateRegSecPage(data).then(res => {
      if(res.data.metaData.code === 'success') {
        message.success('添加成功')
      }
      else {
        message.error(res.data.metaData.message)
      }
    })
  }
}
//
export const getCateResourceInfo = (data) => {
  return dispatch => {
    getResourceInfo(data).then(res => {
      console.log(res.data.data)
    })
  }
}

export const getCateResMetaInfo = (data) => {
  return dispatch => {
    getResourceTableInfo(data).then(res => {
      console.log(res.data.data)
    })
  }
}

export const deleteCateInfo =(data) => {
  return dispatch => {
    deleteResourceById(data).then(res => {
      
    })
  }
}

export const sendResPost = (data) => {
  return dispatch => {
    postResource(data).then(res => {
      
    })
  }
}

export const getMetaTypeList = (requestData) => {
  return dispatch => {
    getAllMetaDataTypes(requestData).then(res => {
      if(res.data.metaData.code === 'success') {
        dispatch(setMetaTypeList(res.data.data))
      }
    })
  }
}

export const setMetaTypeList = (data) => ({
  type: constants.SETMETATYPELIST,
  data: fromJS(data)
})

export const setCateregEditShow = value => ({
  type: constants.SETCATEEDITSHOW,
  value
})